﻿@using NewsMine.DomainObjects;
@using NewsMine.DataManagers;
@using System.Collections.Generic;

 @functions{

     public static List<WebApp1.Controllers.FeedGroup> DevideGroupsForSize(WebApp1.Controllers.FeedGroup group, int size = 8)
        {
            if (group.Feeds.Count < 5)
                return new List<WebApp1.Controllers.FeedGroup> { group };
         
            List<WebApp1.Controllers.FeedGroup> results = new List<WebApp1.Controllers.FeedGroup>();

            int autoSize = 5;
         
            int gCount = group.Feeds.Count;


            for (int i = 0; i < group.Feeds.Count; i += autoSize)
            {
               

                var feeds = group.Feeds.GetRange(i, Math.Min(autoSize, group.Feeds.Count - i));

                if(feeds.Count <= Math.Round(autoSize * 0.3) && results.Count>0)
                {
                    results.Last().Feeds.AddRange(feeds);
                }
                else
                {
                    WebApp1.Controllers.FeedGroup newGroup = new WebApp1.Controllers.FeedGroup
                    {
                        Date = group.Date,
                        Domain = group.Domain,
                        ImageHeight = group.ImageHeight,
                        ImageWidth = group.ImageWidth,
                        ImageUrl = group.ImageUrl,
                        Type = group.Type
                    };
                    newGroup.Feeds = feeds;
                
                    results.Add(newGroup);
                }
            }

            return results;
          
        }
        
        }

@helper DisplayPageTitle()
    {
        var DataFromController = (QResult)Model;

        var tempWebSiteName = string.Empty;

        if (DataFromController.CurrentDomain != null)
        {
            tempWebSiteName = DataFromController.CurrentDomain.LocalName;

            if (string.IsNullOrEmpty(tempWebSiteName))
            {
                tempWebSiteName = DataFromController.CurrentDomain.Name;
            }
        }

        if (DataFromController.CurrentDomain != null)
        {
       <div class="topPageTitle">
       <h1> <img style="max-height:70px;" class="img-rounded" src="@DataFromController.CurrentDomain.Logo1Uri" />  @tempWebSiteName News</h1>
       </div>
        }
        else if (DataFromController.CurrentLanguage != null)
        {
        <div class="topPageTitle">
       <h1> @DataFromController.CurrentLanguage.ToUpperInvariant() News</h1>
       </div>

        }
}


@{
    ViewBag.Title = "Look at news";
    // Layout = "~/Views/Shared/_Layout.cshtml";

    string selectedDomainName = string.Empty;

    QResult DataFromController = (QResult)Model;
    DataFromController.SortWebSitesByAlexaRank();
    bool IsImageDisplayEnabled = ((QResult)Model).IsImageDisplayEnabled;

    List<FeedItem> feedItemsWithImage = new List<FeedItem>();
    List<FeedItem> feedItemsWithOutImage = new List<FeedItem>();

    foreach (FeedItem feedItem in DataFromController.FeedItems)
    {
        if (string.IsNullOrEmpty(feedItem.ImageUrl))
        {
            feedItemsWithOutImage.Add(feedItem);
        }
        else
        {
            feedItemsWithImage.Add(feedItem);
        }
    }


    string previousPageLink = DataFromController.CurrentUrl + "&pgno=" + (DataFromController.PageNumber - 1).ToString();
    string nextPageLink = DataFromController.CurrentUrl + "&pgno=" + (DataFromController.PageNumber + 1).ToString();

    string selectedLanguage = ((QResult)Model).CurrentLanguage;

    Layout = "~/Views/Shared/_3ColumnMasterLayout.cshtml";

    string tempTitle = string.Empty;
    string truncatedTitle = string.Empty;
    int titleDisplaySize = 150;

    string[] availableCategories = WebApp1.Models.CategoryMaster.GetAvailableCategories();

    string isLanguageMenuDisplay = "none";
    string languageMenuHeight = "0px";

    if (((QResult)Model).CurrentLanguageWebsites != null && ((QResult)Model).CurrentLanguageWebsites.Count > 0)
    {
        isLanguageMenuDisplay = "inline";
        languageMenuHeight = "auto";
    }
    bool isAdminPortal = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings.Get("IsAdminPortal"));

    bool isFeedIsAvailable = (((QResult)Model).FeedItems != null && ((QResult)Model).FeedItems.Count > 0);

    var currentDomain = ((QResult)Model).CurrentDomain;

    if (currentDomain != null)
    {

        selectedDomainName = currentDomain.Domain;
    }


    string currentLanguageName = ((QResult)Model).CurrentLanguage;

    int pageNumber = ((QResult)Model).PageNumber;


    var tempWebSiteName = string.Empty;

    if (currentDomain != null)
    {
        tempWebSiteName = DataFromController.CurrentDomain.LocalName;

        if (string.IsNullOrEmpty(tempWebSiteName))
        {
            tempWebSiteName = DataFromController.CurrentDomain.Name;
        }
    }

    int maxItemsInSubMenu = -1;

    var channels = NewsMine.DataManagers.ChannelManager.GetChannelsForMainMenu();

    if (!string.IsNullOrEmpty(currentLanguageName))
    {
        channels = (from ch in channels
                    where !string.IsNullOrEmpty(ch.Language) && ch.Language.ToLower() == currentLanguageName.ToLower()
                    select ch).ToList();

    }
    
    
    
}

<script src="@Url.Content("~/scripts/blocksit.js")?v=@WebApp1.Models.WebAppMetaData.ContentVersion"></script>
<script src="@Url.Content("~/Content/js/PinStyle2Internals.js")?v=@WebApp1.Models.WebAppMetaData.ContentVersion"></script>

<div id="nav-category" style="z-index:200;background-color:#fcfcff;">
    <div style="border:0px solid #fff;">
    <ul class="nav nav-pills" style="margin:0px;">

        @{
            List<NewsMine.DomainObjects.WebSiteInfo> websites = new List<NewsMine.DomainObjects.WebSiteInfo>();

            websites = ((QResult)Model).CurrentLanguageWebsites;

            string websiteName = string.Empty;

            int webSiteCounterTemp = 0;

            foreach (Channel ch in channels)
            {
                if (DataFromController.CurrentChannel != null && !string.IsNullOrEmpty(DataFromController.CurrentChannel.Name)
                    && !string.IsNullOrEmpty(ch.Name)
                    && ch.Name.ToLower() == DataFromController.CurrentChannel.Name.ToLower())
                {
                     <li class="active"><a tabindex="-1"  style="margin:5px;padding-right:5px;font-size:15px;" href="/channel/@ch.Name" >
                    @Html.Raw(ch.DisplayName)
                
                </a></li>
                    
                }
                else
                {
                    <li style=""><a tabindex="-1"  style="margin:5px;padding-right:5px;font-size:15px;" href="/channel/@ch.Name" >
                    @Html.Raw(ch.DisplayName)
                
                </a></li>
                }
            }
            
            }

             <li><button class="btn btn-link" tabindex="-1" style="margin:5px;padding-right:5px;font-size:12px;color:#555" data-toggle="collapse" data-target="#demo">
                <b>More...</b>
            </button></li>
            

             </ul>
            
             <ul class="nav nav-pills" style="margin:0px;">
            @{
                
                for (int i = 0; i < websites.Count; i++)
                {
                    if (i >= maxItemsInSubMenu)
                    {
                        break;
                    }

                    var website = websites[i];

                    websiteName = string.Empty;

                    websiteName = website.LocalName;

                    if (string.IsNullOrEmpty(websiteName))
                    {
                        websiteName = website.Name;
                    }

                    if (!string.IsNullOrEmpty(selectedDomainName) && selectedDomainName.ToLower() == website.Domain.ToLower())
                    {
            <li class="active" style=""><a tabindex="-1"  style="margin:5px;padding-right:5px;font-size:15px;" href="/source/@website.Domain" >
                <img src='https://plus.google.com/_/favicon?alt=feed&domain=@website.Domain ' style="padding-right:8px;width:20px;"/>@Html.Raw(websiteName)
            </a></li>
                    }
                    else
                    {
            <li><a tabindex="-1"  style="margin:5px;padding-right:5px;font-size:15px;" href="/source/@website.Domain" >
                <img src='https://plus.google.com/_/favicon?alt=feed&domain=@website.Domain ' style="padding-right:8px;width:20px;"/>@Html.Raw(websiteName)
            </a></li>
                    }


                }

            
            
             }

             

           

            </ul>
           
    <ul id="demo" class="nav nav-pills collapse" style="margin-bottom:6px;">

            @{
                
                websites = new List<NewsMine.DomainObjects.WebSiteInfo>();

                websites = ((QResult)Model).CurrentLanguageWebsites;

                if (websites.Count > maxItemsInSubMenu + 1)
                {

                    for (int i = maxItemsInSubMenu + 1; i < websites.Count && i < 35; i++)
                    {
                        var website = websites[i];

                        websiteName = string.Empty;

                        websiteName = website.LocalName;

                        if (string.IsNullOrEmpty(websiteName))
                        {
                            websiteName = website.Name;
                        }

                        if (!string.IsNullOrEmpty(selectedDomainName) && selectedDomainName.ToLower() == website.Domain.ToLower())
                        {
            <li class="active" style=""><a tabindex="-1"  style="margin:5px;padding-right:5px;font-size:15px;" href="/source/@website.Domain" >
                <img src='https://plus.google.com/_/favicon?alt=feed&domain=@website.Domain ' style="padding-right:8px;width:20px;"/>@Html.Raw(websiteName)
            </a></li>
                        }
                        else
                        {
            <li><a tabindex="-1"  style="margin:5px;padding-right:5px;font-size:15px;" href="/source/@website.Domain" >
                <img src='https://plus.google.com/_/favicon?alt=feed&domain=@website.Domain ' style="padding-right:8px;width:20px;"/>@Html.Raw(websiteName)
            </a></li>
                        }

                    }



                }



            }

          

   
    </ul>

    </div>
</div>

 @DisplayPageTitle()

<section id="wrapper" style="margin-top:0px;">


   
   
@* <div id="bread-nav" data-spy="affix" data-offset-top="200" style="border:1px solid #fff;font-weight:bolder;z-index:200;">*@


 <div id="bread-nav" style="border:1px solid #fff;font-weight:bolder;z-index:200;">

<ul class="breadcrumb" style="margin-bottom:0px;background-color:rgba(255,255,255,0.95);text-align:left;color:Red;">

@{
    string tempPageInfo = string.Empty;

    if (pageNumber > 0)
    {
        tempPageInfo = "page " + pageNumber.ToString() + "";
    }

    if (string.IsNullOrEmpty(selectedDomainName) && !string.IsNullOrEmpty(currentLanguageName))
    {
    <li><a href="/language/@currentLanguageName">
        @System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(currentLanguageName) News (All websites) </a> 
        <span style="color:#a00;">»</span></li>
        <li class="active"><a>
            @tempPageInfo </a> </li>
    }
    else
        if (!string.IsNullOrEmpty(selectedDomainName) && !string.IsNullOrEmpty(((QResult)Model).CurrentLanguage))
        {
            
    <li><a href="/language/@currentLanguageName">
         @System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(currentLanguageName) News (All websites)</a> <span style="color:#a00;">»</span></li>
    
        
     <li><a href="/source/@selectedDomainName">
      <img src='https://plus.google.com/_/favicon?alt=feed&domain=@selectedDomainName ' style="padding-right:8px;width:20px;"/>
        @tempWebSiteName  </a> <span style="color:#a00;">»</span></li>
    <li class="active"><a>
      
        @tempPageInfo </a></li>
            

        }
        }

</ul>  
 </div>     
 
 
 
 

@if (isFeedIsAvailable == false)
{
    @MyHtmlHelpers.DisplayOopsMessage(this)
}  
   
   
<div id="container"  style="background-color:transparent;margin-left:auto;margin-right:auto;text-align:center;">

       @{
           
           List<FeedItem> filteredFeeds = new List<FeedItem>();

           var feeds = ((QResult)Model).FeedItems;
           
    for (int i = 0; i < feeds.Count; i++)
    {
        feeds[i].Title = System.Text.RegularExpressions.Regex.Replace(feeds[i].Title, @"\s+", " ");
        tempTitle = feeds[i].Title;

        if (!string.IsNullOrEmpty(tempTitle))
        {
            tempTitle = tempTitle.Replace("+", "-");
            tempTitle = NewsMine.Utilities.Extensions.TruncateAtWord(tempTitle, 70);
            tempTitle = HttpUtility.UrlEncode(tempTitle.Replace("+", "-"));
        }

        truncatedTitle = feeds[i].Title;
        titleDisplaySize = 70;
        if (!string.IsNullOrEmpty(truncatedTitle))
        {
            truncatedTitle = NewsMine.Utilities.Extensions.TruncateAtWord(truncatedTitle, titleDisplaySize);
        }

        // this is temp fix, for one of the malayalam channel.
        if (truncatedTitle.Contains("<!--") || truncatedTitle.Contains(".popover();")
            || truncatedTitle.StartsWith("आगे »")
            )
        {
            continue;
        }

        filteredFeeds.Add(feeds[i]);
           
    }

    
    List<WebApp1.Controllers.FeedGroup> results = (from f in filteredFeeds
                                                          group f by new
                                                          {
                                                              f.ImageUrl,
                                                              f.ImageWidth,
                                                              f.ImageHeight,
                                                              f.Domain,
                                                              
                                                          }
                                                              into gfeeds
                                                              select new WebApp1.Controllers.FeedGroup
                                                              {
                                                                  Feeds = gfeeds.ToList(),
                                                                  Date = gfeeds.Max(f => f.PubDate),
                                                                  ImageUrl = gfeeds.Key.ImageUrl,
                                                                  ImageHeight = gfeeds.Key.ImageHeight,
                                                                  ImageWidth = gfeeds.Key.ImageWidth,
                                                                  Domain = gfeeds.Key.Domain
                                                              }).ToList();
                                                              
        // Just split the groups if the feeds are more than 8 feeds in one group.

    List<WebApp1.Controllers.FeedGroup> resultsFinal = new List<WebApp1.Controllers.FeedGroup>();

    foreach (var fGroup in results)
    {
        if (fGroup.Feeds.Count > 8)
        {
            resultsFinal.AddRange(DevideGroupsForSize(fGroup, 8));
        }
        else
        {
            resultsFinal.Add(fGroup);
        }
    }

    resultsFinal = (from feedGroup in resultsFinal
                    orderby feedGroup.Feeds.Max(f => f.PubDate) descending, feedGroup.ImageWidth descending
                    select feedGroup).ToList();
                    

           //List<WebApp1.Controllers.FeedGroup> results = (from f in ((QResult)Model).FeedItems
           //                                               group f by new
           //                                               {
           //                                                   f.Domain,
           //                                                   f.PubDate.Date.Day
           //                                               }
           //                                                   into gfeeds
           //                                                   select new WebApp1.Controllers.FeedGroup
           //                                                   {
           //                                                       Feeds = gfeeds.ToList(),

           //                                                       Domain = gfeeds.Key.Domain
           //                                                   }).ToList();


         
               @MyHtmlHelpers.DisplayFeedsByGroup(resultsFinal, this);
               
                                                
                                          
                                          
@*@MyHtmlHelpers.DisplayFeeds(((QResult)Model).FeedItems, this)*@

           }
           
           
</div>

 @RenderPage("~/views/shared/_pagerView.cshtml")

<hr />

 <div id="websiteContainer" style="width: 1100px;position:relative;margin:0 auto 25px;border-top:#000;">
      <div class="clearfix" style="text-align:left;">
              <ul class="nav nav-pills" style="display: block; position: static; margin-bottom: 5px;">
                @{
                    // var currentDomain = ((QResult)Model).CurrentDomain;
                    //string selectedDomainName = string.Empty;
                    if (currentDomain != null)
                    {
                        selectedDomainName = currentDomain.Domain;
                    }
                    websites = ((QResult)Model).CurrentLanguageWebsites;

                    foreach (var website in websites)
                    {
                        if (isFeedIsAvailable == false)
                        {
                            break;
                        }

                        if (!string.IsNullOrEmpty(selectedDomainName) && selectedDomainName.ToLower() == website.Domain.ToLower())
                        {
                            <li class="active"><a tabindex="-1"  style="margin:5px;padding-right:5px;font-size:15px;" href="/source/@website.Domain" rel="nofollow">
                        <img src='https://plus.google.com/_/favicon?alt=feed&domain=@website.Domain ' style="padding-right:8px;width:20px;"/>@Html.Raw(website.Name)
                    </a></li>
                        }
                        else
                        {
                            <li><a tabindex="-1"  style="margin:5px;padding-right:5px;font-size:15px;" href="/source/@website.Domain" rel="nofollow">
                        <img src='https://plus.google.com/_/favicon?alt=feed&domain=@website.Domain ' style="padding-right:8px;width:20px;"/>@Html.Raw(website.Name)
                    </a></li>
                        }
                    }
            }
              </ul>
            </div>
      @{
                
          List<NewsMine.DomainObjects.WebSiteInfo> websites1 = new List<NewsMine.DomainObjects.WebSiteInfo>();

          websites1 = ((QResult)Model).CurrentLanguageWebsites;

          foreach (var website in websites1)
          {


              continue;
              if (!string.IsNullOrEmpty(website.Logo1Uri))
              {
                <div class="grid" style="border-radius:25px;text-align:center;position:relative;border:0px solid lightgrey;">
                      <a class="name" style="text-decoration:none;text-align:center;left:50%;top:50%;" target="_self" href="/source/@website.Domain">
			        <img style="width:100%;max-height:auto;margin-left:auto;margin-right: auto;" Title="@Html.Raw(website.Name)" src="@website.Logo1Uri" />
                </a>
              </div>

              }
          }
       }
    </div>


</section>
